
import GridLayerListView from "../../../engine/GridLayerListView"
import SDKManager from "../../SDKManager";
import BaseChannel from "../../base/BaseChannel";
import BaseView from "../../../cfw/mvc/BaseView";
import { BannerDir, BattleID } from "../../SDKConfig";
import BigExportPage2Item from "./BigExportPage2Item";
import GameItemModel from "../../base/GameItemModel";
import RandomHelper from "../../../cfw/tools/RandomHelper";
import { ExportType } from "../BigExportC";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BigExportPage2 extends BaseView {

	// @property({ type: cc.ScrollView, displayName: "scrollview$VScrollView" })
	// scrollview$VScrollView: cc.ScrollView = null;

	// @property({ type: GridLayerListView, displayName: "scrollview$VGridLayerListView" })
	// scrollview$VGridLayerListView: GridLayerListView = null;


	@property([BigExportPage2Item])
	list: BigExportPage2Item[] = []
	protected model: BaseChannel;

	protected isJump: boolean = false;

	protected clickCount: number = 0;
	protected temp: GameItemModel[] = []
	onLoad() {
		// let list = this.model.getAdData()
		// this.scrollview$VGridLayerListView.totalCount = list.length
		// console.log(' cc.os ', cc.sys.os)
		this.refresh()
		SDKManager.getChannel().battleStart(BattleID.ENTER_EXPORT2_RETURN_GAME)
	}

	refresh() {
		if (!cc.isValid(this.node)) {
			return
		}
		this.temp.length = 0;
		let list = this.model.getAdData()
		for (let index = 0; index < list.length; index++) {
			const element = list[index];
			this.temp.push(element)

		}
		if (this.temp.length > 0) {
			for (let index = 0; index < this.list.length; index++) {
				const element = this.list[index];
				element.setController(this.controller)
				if (this.temp.length > 0) {
					let i = RandomHelper.random(0, this.temp.length)
					if (this.temp[i]) {
						element.setModel(this.temp[i])
						element.refresh()
						this.temp.splice(i, 1)
					} else {
						element.clear()
					}
				} else {
					element.clear()
				}

			}

		}

		this.setTimeout(() => {
			this.refresh()
		}, 10000);

	}




	onDestroy() {
		super.onDestroy()
		SDKManager.getChannel().battleEnd(BattleID.ENTER_EXPORT2_RETURN_GAME)
	}


	// updateItem(listView, pos: number, item: cc.Node) {
	// 	let list = this.model.getAdData()
	// 	let comp = this.controller.initView(item, 'BigExportPage2Item', list[pos])
	// 	if (comp) {
	// 		comp.refresh();
	// 	}
	// }

	onBackClick() {
		let flag = SDKManager.getChannel().isBannerErrorClick();
		// console.log(' onBackClick flag ', flag)
		if (flag) {
			if (this.clickCount == 0) {
				this.setTimeout(() => {
					if (!cc.isValid(this.node)) {
						return;
					}
					SDKManager.getChannel().showBanner(0, BannerDir.BOTTOM_MID)
					this.setTimeout(() => {
						// if (cc.isValid(this.node)) {
						SDKManager.getChannel().hideBanner()
						// }
					}, 1500);
				}, 1000);
				this.clickCount++;
				return;
			}
			// if (!this.isJump) {
			// 	this.isJump = true;

			// } else {
			this.closeView();
			// }

		} else {
			this.closeView();
		}

	}

	closeView() {

		this.hide()
		this.controller.closeView(ExportType.EXPORT2)
	}


}